package cn.com.lyj6851.modules.user.controller;

import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.user.domain.YdyUser;
import cn.com.lyj6851.modules.user.service.YdyUserService;
import cn.com.lyj6851.modules.user.service.dto.UserMoneyDto;
import cn.com.lyj6851.modules.user.service.dto.YdyUserQueryCriteria;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "商城:会员管理")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("api")
public class MemberController {
    private final YdyUserService userService;

    @AopLog("查询用户")
    @ApiOperation(value = "查询用户")
    @GetMapping(value = "/ydyUser")
    @PreAuthorize("hasAnyRole('admin','USER_ALL','USER_SELECT')")
    public ResponseEntity getUsers(YdyUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(userService.queryAll(criteria,pageable), HttpStatus.OK);
    }
    @AopLog("查看下级")
    @ApiOperation(value = "查看下级")
    @PostMapping(value = "/ydyUser/spread")
    @PreAuthorize("hasAnyRole('admin','USER_ALL','USER_EDIT')")
    public ResponseEntity getSpread(@RequestBody YdyUserQueryCriteria criteria){
        return new ResponseEntity<>(userService.querySpread(criteria.getUid(),criteria.getGrade()),
                HttpStatus.OK);
    }

    @AopLog("修改用户")
    @ApiOperation(value = "修改用户")
    @PutMapping(value = "/ydyUser")
    @PreAuthorize("hasAnyRole('admin','USER_ALL','USER_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdyUser resources){
        userService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @AopLog("删除用户")
    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/ydyUser/{uid}")
    @PreAuthorize("hasAnyRole('admin','USER_ALL','USER_DELETE')")
    public ResponseEntity delete(@PathVariable Integer uid){
        userService.removeById(uid);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "用户禁用启用")
    @PostMapping(value = "/ydyUser/onStatus/{id}")
    public ResponseEntity onStatus(@PathVariable Long id,@RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Integer status = jsonObject.getInteger("status");
        userService.onStatus(id,status);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "修改余额")
    @PostMapping(value = "/ydyUser/money")
    @PreAuthorize("hasAnyRole('admin','USER_ALL','USER_EDIT')")
    public ResponseEntity updatePrice(@Validated @RequestBody UserMoneyDto param){
        userService.updateMoney(param);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
